<template>
  <div>
    <div class="role-list">
      <el-form :inline="true" :model="roleForm" class="demo-form-inline">
        <el-form-item label="角色名称">
          <el-input v-model="roleForm.sysRoleName" placeholder="支持模糊匹配" />
        </el-form-item>
        <el-form-item label="角色类型">
          <el-select v-model="roleForm.sysRoleType" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="管理员" value="0" />
            <el-option label="普通员工" value="1" />
            <el-option label="其他" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="roleForm.sysRoleStatus" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="已启用" value="0" />
            <el-option label="已禁用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="roleAdd">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="roleListArray.list" border style="width: 100%">
        <el-table-column label="角色名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.sysRoleName }}
          </template>
        </el-table-column>
        <el-table-column label="角色类型" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.sysRoleType === '0'" type="danger" disabled>管理员</el-button>
            <el-button v-else-if="scope.row.sysRoleType === '1'" type="success" plain disabled>普通员工</el-button>
            <el-button v-else type="success" plain disabled>普通员工</el-button>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.sysRoleStatus === '0'" type="success" disabled>已启用</el-button>
            <el-button v-else type="danger" plain disabled>已禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="medium " type="danger" @click="handleRoleAuthor(scope.$index, scope.row)">授权</el-button>
            <el-button size="medium " type="primary" @click="handleRoleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="medium " type="warning" @click="handleStatus(scope.$index, scope.row)">状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="roleListArray.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="roleListArray.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="roleListArray.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog
      title="编辑角色"
      :visible.sync="dialogVisibleRoleEdit"
      :close-on-click-modal="false"
      width="30%"
    >
      <auth-role-edit :auth-role-edit-obj="authRoleEditObj" @closeInnerVisibleRoleEdit="closeInnerVisibleRoleEdit" />
    </el-dialog>
    <el-dialog
      title="新增角色"
      :visible.sync="dialogVisibleRoleAdd"
      :close-on-click-modal="false"
      width="30%"
    >
      <auth-role-add :auth-role-add-obj="authRoleAddObj" @closeInnerVisibleRoleAdd="closeInnerVisibleRoleAdd" />
    </el-dialog>
    <el-dialog
      :title="VipAuthorTitle"
      :visible.sync="dialogVisibleRolePermisson"
      :close-on-click-modal="false"
      width="40%"
    >
      <auth-role-permission :auth-role-permission-obj="authRolePermissionObj" @closeInnerVisibleRolePermission="closeInnerVisibleRolePermission" />
    </el-dialog>
  </div>
</template>

<script>
import AuthRoleEdit from './component/AuthRoleEdit'
import Role from '@/api/axios/api/author/role'
import AuthRoleAdd from './component/AuthRoleAdd'
import AuthRolePermission from './component/AuthRolePermission'

export default {
  name: 'AuthVip',
  components: {
    AuthRolePermission,
    AuthRoleAdd,
    AuthRoleEdit
  },
  data() {
    return {
      vipUidObjEdit: {},
      vipUidObjDetail: {},
      vipUidObjRole: {},
      roleListArray: [],
      authRoleEditObj: {},
      authRoleAddObj: {},
      dialogVisibleRoleEdit: false,
      dialogVisibleRoleAdd: false,
      VipAuthorTitle: '',
      dialogVisibleRolePermisson: false,
      authRolePermissionObj: {
        sysRoleUuid: '',
        authMenuTreeData: []
      },
      roleForm: {
        roleUuid: '',
        sysRoleName: '',
        sysRoleStatus: '',
        sysRoleType: '',
        pageSize: 10,
        pageNum: 1
      },
      tableData: {}
    }
  },
  created() {
    this.roleListData()
  },
  methods: {
    handleCurrentChange(pageNum) {
      this.roleForm.pageNum = pageNum
      this.roleListData()
    },
    handleSizeChange(pageSize) {
      this.roleForm.pageSize = pageSize
      this.roleListData()
    },
    onSubmit() {
      this.roleListData()
    },
    closeInnerVisibleRolePermission() {
      this.dialogVisibleRolePermisson = false
    },
    handleRoleAuthor(index, row) {
      this.VipAuthorTitle = '正在为 ' + row.sysRoleName + ' 授权'
      this.dialogVisibleRolePermisson = true
      Role.roleHaveAclTree(row).then(res => {
        this.authRolePermissionObj.authMenuTreeData = res.data
        this.authRolePermissionObj.sysRoleUuid = row.sysRoleUuid
      })
    },
    roleAdd() {
      this.dialogVisibleRoleAdd = true
    },
    handleRoleEdit(index, row) {
      Role.roleDetail(row).then(res => {
        this.authRoleEditObj = res.data
        this.dialogVisibleRoleEdit = true
      })
    },
    closeInnerVisibleRoleEdit(param) {
      this.dialogVisibleRoleEdit = false
      this.roleListData()
    },
    closeInnerVisibleRoleAdd(param) {
      this.dialogVisibleRoleAdd = false
      this.roleListData()
    },
    handleStatus(index, row) {
      let msg = '启用'
      if (row.userGroupStatus === '0') {
        msg = '禁用'
      }
      this.$confirm('您确定要 ' + msg + ' ' + row.sysRoleName + ' 吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Role.roleStatus(row).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: res.data
            })
            setTimeout(() => {
              this.roleListData()
            }, 1000)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消对 ' + row.sysRoleName + ' 的状态变更'
        })
      })
    },
    roleListData() {
      Role.roleListData(this.roleForm).then(res => {
        this.roleListArray = res.data
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
  .role-list
    padding 18px
    overflow hidden
</style>
